package com.wh.influxdb.common.core;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.domain.WritePrecision;
import com.wh.influxdb.domain.vo.Page;
import com.wh.influxdb.measurement.Stress;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * @Author xu
 * @Date 2025/1/15
 **/
@Slf4j
public class InfluxdbTemplate {

    @Autowired
    private InfluxDBClient influxDBClient;

    private final String fluxFrom;

    public InfluxdbTemplate(String bucket) {
        this.fluxFrom = "from(bucket: \"" + bucket + "\") |> ";
    }

    public <T> List<T> queryMByFlux(String query, Class<T> clazz) {
        log.info("flux: {}", query);
        if (StringUtils.isBlank(query)) {
            return Collections.emptyList();
        }
        return influxDBClient.getQueryApi().query(this.fluxFrom + query, clazz);
    }

//    public <T> Page<T> page(LambdaQuery<T> query , Page<T> page) {
//        return page;
//    }

    public void batchInsert(WritePrecision writePrecision, List<?> entity) {
        influxDBClient.getWriteApiBlocking().writeMeasurements(writePrecision, entity);
    }

    public <T> void insert(WritePrecision writePrecision, T entity) {
        influxDBClient.getWriteApiBlocking().writeMeasurement(writePrecision, entity);
    }

}